﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using titi.utilmouse;


// REF : http://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c

namespace WindowsFormsApplication1
{
    // TYPE 1
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;
        private FormText ftext = new FormText();

        private List<IListItem> ps = new List<IListItem>() ;

        public void DoMouseClick()
        {
            //Call the imported function with the cursor's current position
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            Form1.mouse_event((uint)(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP)
                                , (uint)X, (uint)Y, 0, 0);
        }

        // ========= MAIN CODE ====== 

        public Form1()
        {
            InitializeComponent();
            //this.TopMost = true;
            this.KeyPreview = true;
        }

        private void btGetPosition_Click(object sender, EventArgs e)
        {
            MouseOperations mop = new MouseOperations();
            MouseOperations.MousePoint lpmp =  mop.GetCursorPosition();
            String sxy = lpmp.X + "," + lpmp.Y;
            lbmsg1.Text = sxy ;
            //mop.SetCursorPosition(10, 10);
            // clear 
            mop = null;
        }

            
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            String ch  = e.KeyChar.ToString();
            if (ch == "s")
            {

                MouseOperations mop = new MouseOperations();
                MouseOperations.MousePoint lpmp = mop.GetCursorPosition();
                String sxy = lpmp.X + "," + lpmp.Y;
                lbmsg1.Text = sxy;
                ps.Add(new ListItemMouseClick(lpmp)); // add to list 
                // clear 
                
                // display list 
                displaylist();

                mop.MouseLeftClick(lpmp);
                SendKeys.SendWait("%{TAB}") ;  // switch back to this 

                mop = null;

                // add to list 
                //Console.WriteLine(" Must Implement .. Add List ..");
            }else if(ch == "k"){
                MouseOperations mop = new MouseOperations();
                MouseOperations.MousePoint lpmp = mop.GetCursorPosition();
                mop = null;
                ftext.ShowDialog();
                if (ftext.isOK())
                {
                    ps.Add(new ListItemClickAndKeyText(lpmp,ftext.getText()));
                    displaylist();
                }
            }else if (ch == "e")
            {
                MouseOperations mop = new MouseOperations();
                MouseOperations.MousePoint lpmp = mop.GetCursorPosition();
                mop = null;

                ftext.ShowDialog();
                if (ftext.isOK())
                {
                    ps.Add(new ListItemClickAndKeyTextEnter(lpmp, ftext.getText()));
                    displaylist();
                }
            }
        }
        private void displaylist()
        {
            listBox1.Items.Clear();
            ps.ForEach(delegate(IListItem lpli )
            {
                listBox1.Items.Add(lpli.getText());
            });
            listBox1.SelectedIndex = listBox1.Items.Count - 1 ;    // move to last item
        }

        // Run Mouse Move
        private void button1_Click(object sender, EventArgs e)
        {
            //this.Hide();
            SendKeys.Send("%{TAB}");
            MouseOperations mop = new MouseOperations();
            mop.Delaynum = (int)numericUpDown1.Value;
            int i = 0;
            ps.ForEach(delegate(IListItem lpli)
            {
                listBox1.SelectedIndex = i ;
                i++; 
                //Console.Write("Run : " + "#" + i + " " + lpli.getText());
                lpli.run(ref mop);
                
                //Console.WriteLine(" .. finished. ");
                //mop.MouseLeftClick(lpmp);
               // System.Threading.Thread.Sleep(1000);
                delay();
            });
            mop = null;

            SendKeys.Send("%{TAB}");
            //SendKeys.SendWait(" ...{ENTER}.. OK form1 again ");
            //this.Show();
            //MessageBox.Show(" Finished Script ");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
			            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //AddClickText(10, 10, "TES");

            // travel to localhost/apt 
            // user must logoff first 
            // must focust on webbrowser then use ALT-TAB to switch to program !!
            //  before run this program !!

            // Script Generated at 25/5/2556 0:00

            // Script Generated at 29/5/2556 0:00 , travel all page of titip

            /* 1. */
            AddClickText(236, 260, "titipong.s");
            /* 2. */
            AddClickTextEnter(297, 293, "1234");
            /* 3. */
            AddMouseClick(152, 262);
            /* 4. */
            AddMouseClick(178, 264);
            /* 5. */
            AddMouseClick(207, 265);
            /* 6. */
            AddMouseClick(242, 264);
            /* 7. */
            AddMouseClick(140, 441);
            /* 8. */
            AddMouseClick(11, 45);
            /* 9. */
            AddMouseClick(139, 466);
            /* 10. */
            AddMouseClick(270, 259);
            /* 11. */
            AddMouseClick(367, 304);
            /* 12. */
            AddMouseClick(136, 240);
            /* 13. */
            AddMouseClick(322, 261);
            /* 14. */
            AddMouseClick(157, 419);
            /* 15. */
            AddMouseClick(19, 44);
            /* 16. */
            AddMouseClick(245, 432);
            /* 17. */
            AddMouseClick(18, 36);
            /* 18. */
            AddMouseClick(283, 452);
            /* 19. */
            AddMouseClick(7, 44);
            /* 20. */
            AddMouseClick(296, 468);
            /* 21. */
            AddMouseClick(22, 47);
            /* 22. */
            AddMouseClick(337, 483);
            /* 23. */
            AddMouseClick(396, 353);
            /* 24. */
            AddMouseClick(66, 442);
            /* 25. */
            AddMouseClick(21, 46);
            /* 26. */
            AddMouseClick(21, 46);
            /* 27. */
            AddMouseClick(444, 495);
            /* 28. */
            AddMouseClick(8, 32);
            /* 29. */
            AddMouseClick(473, 522);
            /* 30. */
            AddMouseClick(18, 39);
            /* 31. */
            AddMouseClick(395, 548);
            /* 32. */
            AddMouseClick(15, 49);
            /* 33. */
            AddMouseClick(453, 557);
            /* 34. */
            AddMouseClick(15, 44);
            /* 35. */
            AddMouseClick(418, 581);
            /* 36. */
            AddMouseClick(19, 38);
            /* 37. */
            AddMouseClick(384, 264);
            /* 38. */
            AddMouseClick(186, 331);
            /* 39. */
            AddMouseClick(341, 336);
            /* 40. */
            AddMouseClick(336, 330);
            /* 41. */
            AddMouseClick(430, 330);
            /* 42. */
            AddMouseClick(327, 370);
            /* 43. */
            AddMouseClick(401, 269);
            /* 44. */
            AddMouseClick(22, 41);
            /* 45. */
            AddMouseClick(429, 269);
            /* 46. */
            AddMouseClick(8, 49);
            /* 47. */
            AddMouseClick(457, 262);
            /* 48. */
            AddMouseClick(153, 261);
            /* 49. */
            AddMouseClick(98, 586);
            /* 50. */
            AddMouseClick(12, 47);
            /* 51. */
            AddMouseClick(95, 615);
            /* 52. */
            AddMouseClick(12, 36);
            /* 53. */
            AddMouseClick(128, 650);
            /* 54. */
            AddMouseClick(189, 366);
            /* 55. */
            AddMouseClick(15, 48);
            /* 56. */
            AddMouseClick(457, 268);
            /* 57. */
            AddMouseClick(363, 330);
            /* 58. */
            AddMouseClick(1, 51);
            /* 59. */
            AddMouseClick(178, 266);
            /* 60. */
            AddMouseClick(403, 237);
            /* 61. */
            AddMouseClick(258, 238);
            /* 62. */
            AddMouseClick(96, 256);


            displaylist();
        }

        private void delay( )
        {
        	System.Threading.Thread.Sleep( (int)numericUpDown1.Value );
        }
        private void AddMouseClick( int x , int y )
        {
            ps.Add(new ListItemMouseClick(new MouseOperations.MousePoint(x, y)));
        }

        private void AddClickText(int x, int y , String s)
        {
            ps.Add(new ListItemClickAndKeyText(new MouseOperations.MousePoint(x,y), s));
        }
        private void AddClickTextEnter(int x, int y, String s)
        {
            ps.Add(new ListItemClickAndKeyTextEnter(
                    new MouseOperations.MousePoint(x, y), s)
                   );
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String s = "// Script Generated at " 
                + DateTime.Today.ToShortDateString() + " " 
                + DateTime.Today.ToShortTimeString();
            s += "\r\n\r\n";
            int cmd_no = 0; 
            ps.ForEach(delegate(IListItem lpli)
            {
                cmd_no++;
                //Console.Write("Run : " + "#" + i + " " + lpli.getText());
                s += "/* " + cmd_no + ". */ \t " + lpli.getCmd() + "\r\n";
                //Console.WriteLine(" .. finished. ");
                //mop.MouseLeftClick(lpmp);
                // System.Threading.Thread.Sleep(1000);
                //delay();
            });
            Clipboard.SetText(s, TextDataFormat.Text);
            MessageBox.Show(" Copy code to Clipboard OK");

        }
        
        // AutoIT v3 Script
        void Button4Click(object sender, EventArgs e)
        {
            String s = "// Script Generated at " 
                + DateTime.Today.ToShortDateString() + " " 
                + DateTime.Today.ToShortTimeString();
            s += "\r\n\r\n";
            int cmd_no = 0; 
            ps.ForEach(delegate(IListItem lpli)
            {
                cmd_no++;
                //Console.Write("Run : " + "#" + i + " " + lpli.getText());
                s += "; " + cmd_no + ".  \r\n " + lpli.getCmdAutoItV3() + "\r\n";
                //Console.WriteLine(" .. finished. ");
                //mop.MouseLeftClick(lpmp);
                // System.Threading.Thread.Sleep(1000);
                //delay();
            });
            Clipboard.SetText(s, TextDataFormat.Text);
            MessageBox.Show(" Copy code to Clipboard OK");        		
        }
        
        
        
        void Timer1Tick(object sender, EventArgs e)
        {
        	titi.utilmouse.MouseOperations mo = new MouseOperations();
        	MouseOperations.MousePoint mp =  mo.GetCursorPosition();
        	Point pt = new Point();
        	pt = new Point(mp.X,mp.Y);
        	Color c = titi.utilcanvas.SceenPixel.GetPixelColor(pt);
        	//this.Text = 
        	lbColor.BackColor = c ;         	
        	
        	
        	Color negativeColor =  Color.FromArgb(0xFFFFFF ^ c.ToArgb());
        	lbColor.ForeColor = negativeColor;
        	lbColor.Text =  "RGB={" + c.R + "," + c.G + "," + c.B +"}";
        }
        
        void Button5Click(object sender, EventArgs e)
        {
        	
//        	titi.utilmouse.MouseMath mmath  = new MouseMath();
//        	List<Point> pts = 	mmath.GetPointsInLine(this.Location,new Point(800,400));
//        	this.Text = pts.Count.ToString();
//        	for(int i = 0; i < pts.Count ; i ++ ){
//        	{	
//        			this.Location = pts[i];
//	                                    	
//        			System.Threading.Thread.Yield();
//        	}
        	titi.MathLocation.MathPoint mp = new titi.MathLocation.MathPoint();
        	mp.MoveFormToAnimate(this.Location,new Point(800,400) ,this);
	                                         
//        	this.Location = new Point(800,400);
//        	titi.utilmouse.MouseOperations mo = new MouseOperations();
//        	mo.SetCursorPosition(100,100);
//        	mo.MouseLeftClick(100,100 );
//        	titi.utilkey.SimKeyPress.KeyPress_CtrlA_Then_CtrC();
//        	String data = Clipboard.GetText();
//        	
//        	
//        	SendKeys.SendWait("%{TAB}");
//        	this.Show();
//        	this.Focus();
//
//        	mo.SetCursorPosition(100,100);
//        	mo.MouseLeftClick(100,100 );
//
//        	MessageBox.Show("OK data in clipboard is \n====================\n"
//        	                + data );
        	
        }
    }
}
